package com.example.config;

import com.example.interceptor.MyInterceptor;
import com.example.interceptor.MyInterceptor1;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.*;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: ws
 * Date: 2024-03-02
 * Time: 17:13
 */

@Configuration
@EnableWebMvc
@ComponentScan("com.example")
public class MVCConfig implements WebMvcConfigurer {





    // 配置自己的拦截器


    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        // 1. 拦截全部的请求
//        registry.addInterceptor(new MyInterceptor());
        // 2. 指定地址拦截
        // * 任意一层，** 任意多层
//        registry.addInterceptor(new MyInterceptor())
//                .addPathPatterns("/user/**");
        // 3. 排除拦截
        // 排除的地址应该在拦截的地址之中
//        registry.addInterceptor(new MyInterceptor())
//                .addPathPatterns("/user/**").excludePathPatterns("/user/data1");

        // 4. 有多个拦截器，先声明的先执行
        registry.addInterceptor(new MyInterceptor());
        registry.addInterceptor(new MyInterceptor1());
        /*
        执行顺序
        MyInterceptor.preHandle()
            MyInterceptor1.preHandle()
                contoller
            MyInterceptor1.postHandle()
        MyInterceptor.postHandle()
            MyInterceptor1.afterCompletion()
        MyInterceptor.afterCompletion()
         */



    }

    // 开启静态资源查找
    // 1. dispatcherServlet 先去 handlerMapper 中找对应的 handler
    // 2. 找不到，再去寻找静态资源
    @Override
    public void configureDefaultServletHandling(DefaultServletHandlerConfigurer configurer) {
        configurer.enable();// 开启该功能
    }
}
